﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.CoQuanCapVanBanThamDuyetForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using ComputerVision.VanBanThamDuyetForm;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmThamDuyetDetail : frmDetail
    {
        private readonly IService<ThamDuyetPCCC> _thamDuyetPCCCService;
        private readonly IService<VanBanThamDuyetPCCC> _vanBanThamDuyetPCCC;
        private readonly IService<CoQuanCapVanBanThamDuyet> _coQuanCap;
        private readonly IUserService<aspnet_Users> _userService;
        public Guid IdPhieu;

        public frmThamDuyetDetail()
        {
            InitializeComponent();
            _thamDuyetPCCCService = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            _coQuanCap = Program.Container.Resolve<IService<CoQuanCapVanBanThamDuyet>>();
            _vanBanThamDuyetPCCC = Program.Container.Resolve<IService<VanBanThamDuyetPCCC>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Thẩm duyệt PCCC";
            LoadCoQuanCap();
            LoadVanBanThamDuyet();
            txtSoVanBan.Focus();
        }

        public frmThamDuyetDetail(Guid id)
        {
            InitializeComponent();
            _thamDuyetPCCCService = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            _coQuanCap = Program.Container.Resolve<IService<CoQuanCapVanBanThamDuyet>>();
            _vanBanThamDuyetPCCC = Program.Container.Resolve<IService<VanBanThamDuyetPCCC>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Thẩm duyệt PCCC";
            LoadCoQuanCap();
            LoadVanBanThamDuyet();
            txtSoVanBan.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _thamDuyetPCCCService.Detail(id);
            if (item != null)
            {
                drlVanBanThamDuyet.SelectedValue = item.VanBanThamDuyet_Id;
                drlCoQuanCap.SelectedValue = item.CoQuanCapVanBanThamDuyet_Id;
                txtSoVanBan.Text = item.SoVanBan;
                txtGhiChu.Text = item.GhiChu;
                dtNgayCap.Value = item.NgayCap.Date;
            }
        }

        private void btnAddVanBanThamDuyet_Click(object sender, EventArgs e)
        {
            var frm = new frmVanBanThamDuyetDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCoQuanCap_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanCapVanBanThamDuyetDetail(this);
            frm.ShowDialog();
        }

        public void LoadVanBanThamDuyet()
        {
            drlVanBanThamDuyet.DataSource = _vanBanThamDuyetPCCC.ToList().ToList();
            drlVanBanThamDuyet.DisplayMember = "Ten";
            drlVanBanThamDuyet.ValueMember = "Id";
        }

        public void LoadCoQuanCap()
        {
            drlCoQuanCap.DataSource = _coQuanCap.ToList().ToList();
            drlCoQuanCap.DisplayMember = "Ten";
            drlCoQuanCap.ValueMember = "Id";
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtSoVanBan.Text))
            {
                errorProvider.SetError(txtSoVanBan, "Bạn chưa nhập số văn bản. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (drlCoQuanCap.SelectedValue ==  null)
            {
                errorProvider.SetError(txtSoVanBan, "Bạn chưa chọn cơ quan cấp. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (drlVanBanThamDuyet.SelectedValue ==  null)
            {
                errorProvider.SetError(txtSoVanBan, "Bạn chưa chọn văn bản thẩm duyệt. Xin vui lòng kiểm tra lại");
                flag = false;
            }
            
            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var vanban = drlVanBanThamDuyet.SelectedItem as VanBanThamDuyetPCCC;
                var coquan = drlCoQuanCap.SelectedItem as CoQuanCapVanBanThamDuyet;
                var model = new ThamDuyetPCCC
                {
                    Id = Guid.NewGuid(),
                    SoVanBan = txtSoVanBan.Text,
                    GhiChu = txtGhiChu.Text,
                    VanBanThamDuyet_Id = vanban.Id,
                    CoQuanCapVanBanThamDuyet_Id = coquan.Id,
                    NgayCap = dtNgayCap.Value.Date,
                    PhieuDieuTraCoSoId = this.IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _thamDuyetPCCCService.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _thamDuyetPCCCService.Detail(guid);
            var vanban = drlVanBanThamDuyet.SelectedItem as VanBanThamDuyetPCCC;
            var coquan = drlCoQuanCap.SelectedItem as CoQuanCapVanBanThamDuyet;
            if (item != null)
            {
                item.SoVanBan = txtSoVanBan.Text;
                item.GhiChu = txtGhiChu.Text;
                item.NgayCap = dtNgayCap.Value.Date;
                item.VanBanThamDuyet_Id = vanban.Id;
                item.CoQuanCapVanBanThamDuyet_Id = coquan.Id;
                //item.PhieuDieuTraCoSoId = txtMa.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _thamDuyetPCCCService.Update(item);
            }
        }

        #endregion

    }
}
